package com.oocl.ita.checks.collector;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.dom4j.DocumentException;

import com.oocl.ita.ServiceIpml.ChecksServiceImpl;
import com.oocl.ita.ServiceIpml.ReceiveShopServiceIpml;
import com.oocl.ita.po.Checks;
import com.oocl.ita.service.ChecksService;
import com.oocl.ita.service.ReceiveShopService;
import com.oocl.ita.util.CheckXMLUtil;

public class JMSMsgReceive implements Runnable{
	private Session session;
	private Destination queue;
	
	public JMSMsgReceive(Session session, Destination queue){
		this.session = session;
		this.queue = queue;
	}
	
	public void run() {
		MessageConsumer mc = null;
		try {
			mc = session.createConsumer(queue);
			for(int i = 0; i < 10; i++){
				TextMessage textMsg = (TextMessage) mc.receive();
				if (textMsg != null){
					String msg = textMsg.getText();
					ChecksService checksService = new ChecksServiceImpl();
					try{
						checksService.addChecks(CheckXMLUtil.XMLToCheck(msg));
					}catch (Exception e) {
						continue;
					}				
					session.commit();
				}else{
					continue;
				}			
			}
		} catch (JMSException e){
			System.out.println("JMSMsgReceive has error!");
		}finally {
			try {
				mc.close();
				session.close();
			} catch (JMSException e) {
				// TODO Auto-generated catch block
				System.out.println("JMSMsgReceive session can't Close!");
			}
		}	
	}
}
